package com.lwz.laboratory.controller;

import com.lwz.laboratory.entity.Department;
import com.lwz.laboratory.entity.Laboratory;
import com.lwz.laboratory.entity.Technician;
import com.lwz.laboratory.service.DepartmentService;
import com.lwz.laboratory.service.LaboratoryService;
import com.lwz.laboratory.service.TechnicianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

@Controller
public class LaboratoryController {
    @Autowired
    LaboratoryService laboratoryService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    TechnicianService technicianService;

    @GetMapping("/laboratorylist")
    public String lablist(Model model, HttpServletRequest request){
        String labname = request.getParameter("context");
        List<Laboratory> list= laboratoryService.getAlllab(labname);
        model.addAttribute("laboratorylist",list);
        return "/laboratory/laboratory_list";
    }

    @RequestMapping("/laboratorylist/{id}")
    public String toupdatelaboratory(String tecname,@PathVariable("id")Integer id, Model model){
        Laboratory laboratory=  laboratoryService.getonelab(id);
        model.addAttribute("laboratory",laboratory);
        List<Technician> technicians =technicianService.getAlltec(tecname);
        model.addAttribute("technicians",technicians);
        return "/laboratory/laboratory_edit";
    }

    @RequestMapping("/laboratoryedit")
    public String editlaboratory(Laboratory laboratory){

        laboratoryService.Updatelab(laboratory);
        return "redirect:/laboratorylist";
    }

    @RequestMapping("/laboratoryadd")
    public String toAddlaboratory( String tecname, HttpServletRequest request,Model model){

        List<Technician> technicians =technicianService.getAlltec(tecname);
        model.addAttribute("technicians",technicians);

        return "/laboratory/laboratory_add";
    }

    @RequestMapping("/labsave")
    public String Addlaboratory(Laboratory laboratory, HttpServletRequest request){
        laboratoryService.Addlab(laboratory);
        return "redirect:/laboratorylist";
    }


    @RequestMapping("/laboratorydel/{id}")
    public String dellaboratory(@PathVariable("id")Integer id, Model model, HttpServletRequest request){
        laboratoryService.Dellab(id);
        return "redirect:/laboratorylist";
    }
}
